package com.surveycomp.servlets;

import java.io.IOException;

import javax.jdo.PersistenceManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.google.appengine.api.datastore.Key;
import com.surverycomp.models.User;
import com.surveycomp.datastore.PMF;

public class CreateUserServlet extends BaseServlet {
	private static final long serialVersionUID = 2005200441592471609L;
	protected PersistenceManager pm = PMF.get().getPersistenceManager();

	@Override
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		// Session was created in FaceChallengeServlet
		HttpSession session = req.getSession();
		String phoneNumber = req.getParameter("phone_number");
		Key eventKey = (Key) session.getAttribute("eventKey");

		User u = new User(phoneNumber, eventKey);
		u = pm.makePersistent(u);
		session.setAttribute("user", u);

		try {
			getServletContext().getRequestDispatcher("/facechallenge").forward(
					req, resp);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
